package com.lq.JDBC.Utils;

import java.sql.Connection;
import java.sql.SQLException;

public class Transaction {
    //开启事务
    public static void begin() {
        Connection connection = DBUtils.getConnection();
        try {
            connection.setAutoCommit(false);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    //提交的方法
    public static void commit() {
        Connection connection = DBUtils.getConnection();
        try {
            connection.commit();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public static void rollback() {
        Connection connection = DBUtils.getConnection();
        try {
            connection.rollback();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void close() {
        Connection connection = DBUtils.getConnection();
        try {
            connection.close();
            //连接关闭以后一定要释放threadLocal的资源
            DBUtils.THREADLOCAL.remove();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
